package com.lry.basic.algorithm.common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2019/9/9
 */
public class SubSets {
    public static void main(String[] args) {
        System.out.println(subsets(new int[]{1,2,3}));
    }
    //000 001 010 011 100 101 110 111
    public static List<List<Integer>> subsets(int[] nums) {
        if(null==nums)return null;
        int total = 1<<nums.length;//8
        List<List<Integer>> resultList = new ArrayList<>();
        for(int i=0;i<total;i++){//0~7
            List<Integer>subList = new ArrayList<>();
            for(int j=0;j<nums.length;j++){//0~2
                //关键之处
                if(((i>>j)&1)==1){
                    subList.add(nums[nums.length-1-j]);
                }
            }
            resultList.add(subList);
        }
        return resultList;
    }

    //1 1 2


    public static List<List<Integer>> subsets2(int[] nums) {
        if(null==nums)return null;
        Arrays.sort(nums);
        int total = 1<<nums.length;//8
        List<List<Integer>> resultList = new ArrayList<>();
        for(int i=0;i<total;i++){//0~7
            List<Integer>subList = new ArrayList<>();
            boolean f = true;
            for(int j=0;j<nums.length;j++){//0~2
                //关键之处
                if(((i>>j)&1)==1){
                    if(j>0&&nums[j]==nums[j-1]&&((i>>(j-1))&1)==0){
                        f = false;
                        break;
                    }else{
                        subList.add(nums[j]);
                    }
                }
            }
            if(f)
            resultList.add(subList);
        }
        return resultList;
    }
}
